In [2]:
%matplotlib inline

Softmax


In [1]:
"""Softmax"""


Out[1]:
'Softmax'

In [3]:
scores = [3.0, 1.0, 0.2]

In [4]:
import numpy as np

In [5]:
def softmax(x):
    """Compute softmax values for x."""
    y = np.exp(x)/np.sum(np.exp(x), axis=0)
    return y

In [6]:
print(softmax(scores))


[ 0.8360188   0.11314284  0.05083836]

In [7]:
# Plot softmax curves
import matplotlib.pyplot as plt
x = np.arange(-2.0, 6.0,0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

In [8]:
plt.plot(x, softmax(scores).T, linewidth=2)


Out[8]:
[<matplotlib.lines.Line2D at 0x7fcb85d39c90>,
 <matplotlib.lines.Line2D at 0x7fcb85d39e90>,
 <matplotlib.lines.Line2D at 0x7fcb85d39fd0>]

In [9]:
plt.show()

In [13]:
# Multiply the scores by 10.  What happens?
scores = np.array([3.0, 1.0, 0.2])
print(softmax(scores* 10))


[  9.99999998e-01   2.06115362e-09   6.91440009e-13]

In [14]:
# Divide the scores by 10.  What happens?
scores = np.array([3.0, 1.0, 0.2])
print(softmax(scores/ 10))


[ 0.38842275  0.31801365  0.2935636 ]

Numerical Stability

EY : 20170126 Numerical Stability!!!
cf. Quiz: Numerical Stability


In [15]:
N = 1000000000
a = 0.000001
ITER_MAX = 1000000

In [16]:
for iter in range(ITER_MAX):
    N += a

In [17]:
print( N - 1000000000 )


0.953674316406

In [18]:
from decimal import Decimal

In [19]:
N_Decimal = Decimal(N)
a_Decimal = Decimal(a)
for iter in range(ITER_MAX):
    N_Decimal += a_Decimal

In [20]:
print( N_Decimal - Decimal( N))


1.000000000000000000

In [ ]: